package cn.nextProject.service.impl;

import cn.nextProject.entity.RoleGroupPermission;
import cn.nextProject.mapper.RoleGroupPermissionMapper;
import cn.nextProject.service.RoleGroupPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class RoleGroupPermissionServiceImpl implements RoleGroupPermissionService {

    @Autowired
    private RoleGroupPermissionMapper roleGroupPermissionMapper;

    @Cacheable(value = "roleGroupPermission",
            key = "(#roleGroupId != null ? #roleGroupId : '') + '_' + " +
                    "(#path != null ? #path : '') + '_'"
    )
    @Override
    public List<RoleGroupPermission> findByRoleGroup(Long roleGroupId, String path) {
        List<RoleGroupPermission> returnList = new ArrayList<>();
        if (!ObjectUtils.isEmpty(roleGroupId)){
            returnList = roleGroupPermissionMapper.findByRoleGroup(roleGroupId, path);
        }
        return returnList;
    }

    @CacheEvict(value = {"permissionList","permissions","permissionListAll","permissionMap","user","roleGroupPermission"}, allEntries = true, beforeInvocation = true)
    @Override
    public void saveBatch(Long roleGroupId, List<Long> permissionIdList) {
        roleGroupPermissionMapper.saveBatch(roleGroupId, permissionIdList);
    }

    @CacheEvict(value = {"permissionList","permissions","permissionListAll","permissionMap","user","roleGroupPermission"}, allEntries = true, beforeInvocation = true)
    @Override
    public void deleteRoleGroupPermission(Long id) {
        roleGroupPermissionMapper.deleteRoleGroupPermission(id);
    }
}
